package com.h3bpm.kumhosunny.comment.controller;

import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.service.factory.ServiceFactory;
import com.h3bpm.base.exception.GlobalErrorConst;
import com.h3bpm.base.res.ResBody;
import com.h3bpm.kumhosunny.comment.beans.WorkflowComment;
import com.h3bpm.kumhosunny.comment.service.WorkflowCommentService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zhengyj
 * @date 2019/8/29
 * @description 流程评论
 */
@Controller
@RequestMapping({"/Portal/Workflow"})
public class WorkFlowCommentController extends ControllerBase {
    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBase.class);
    private WorkflowCommentService workflowCommentService = (WorkflowCommentService) ServiceFactory.getService(WorkflowCommentService.class);

    public WorkFlowCommentController() {
    }

    @Override
    public String getFunctionCode() {
        return null;
    }

    @RequestMapping(
            value = {"/WorkflowComments"},
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public ResBody getComments(@RequestParam String instanceId) {
        if (StringUtils.isEmpty(instanceId)) {
            LOGGER.error("InstanceId can't be null or empty.");
            return ResBody.buildFailResBody(GlobalErrorConst.buildBizErrorConst("report.configNotExist"));
        } else {
            return this.workflowCommentService.getComments(instanceId);
        }
    }

    @RequestMapping(
            value = {"/addWorkflowComment"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ResBody addComment(@RequestBody WorkflowComment comment) {
        return this.workflowCommentService.addComment(comment);
    }

    @RequestMapping(
            value = {"/uploadWorkflowCommentAttachment"},
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public ResBody uploadAttachment(@RequestParam("file") MultipartFile file, boolean uploadByImage) {
        return this.workflowCommentService.uploadAttachment(file, uploadByImage);
    }

}

